package ssm.com.serviceImpl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ssm.com.mapper.DoorMapper;
import ssm.com.pojo.Door;
import ssm.com.service.DoorService;

import java.util.List;

//业务层实现类
@Service
public class DoorServiceImpl implements DoorService {

    //在业务层如何调用持久层
    //使用一个自动装配的注解，将spring容器管理的对象注入进来

    //把持久层对象注入到业务层中
    @Autowired//自动装配(required = false)
    DoorMapper doorMapper;

    @Override
    public int addDoorInfo(Door door){
        if (door == null) {
            System.out.println("添加失败");
            return 0;
        }

        //调用持久层方法
        int row = doorMapper.addDoor(door);
        if (row>0) {
            return row;
        }else {
            System.out.println("添加失败");
            return 0;
        }

    }

    @Override
    public List<Door> findAllDoorInfo() {
        List<Door> list = doorMapper.findAllDoorInfo();
        if (list.size()>0) {
            return list;
        } else {
            return null;
        }

    }

    @Override
    public int deleteDoorById(Integer id) {
        int row = doorMapper.deleteDoorById(id);
        if (row<=0) {
            return 0;
        }
        return row;
    }

    @Override
    public Door findDoorByIdUpdate(Integer id) {
        if (id==null) {
            throw new RuntimeException("id不能为空");
        }

        //使用持久层对象调用持久层方法
        Door door = doorMapper.findDoorByIdUpdate(id);
        return door;
    }

    @Override
    public int toUpdateDoor(Door door) {
        if (door==null) {
            return 0;
        }
        int row = doorMapper.toUpdateDoor(door);
        if (row<=0) {
            return 0;
        }
        return row;
    }


}
